package com.doubao.notepad_blog_backend.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.doubao.notepad_blog_backend.common.api.ApiResult;
import com.doubao.notepad_blog_backend.domain.BmsPost;
import com.doubao.notepad_blog_backend.domain.UmsUser;
import com.doubao.notepad_blog_backend.dto.LoginDTO;
import com.doubao.notepad_blog_backend.dto.RegisterDTO;
import com.doubao.notepad_blog_backend.service.BmsPostService;
import com.doubao.notepad_blog_backend.service.UmsUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;

import static com.doubao.notepad_blog_backend.jwt.JwtUtil.USER_NAME;

@RestController
@RequestMapping("/auth/user")
@Api(value = "用户管理",tags = {"用户管理"})
public class UmsUserController {

    @Resource
    private UmsUserService umsUserService;

    @Resource
    private BmsPostService bmsPostService;

    @ApiOperation( "注册" )
    @PostMapping("/register")
    private ApiResult<Map<String,Object>> register(@Valid  @RequestBody RegisterDTO registerDTO) {
        UmsUser user = umsUserService.register(registerDTO);
        if(ObjectUtils.isEmpty( user )){
            return ApiResult.failed( "账号注册失败" );
        }
        HashMap<String, Object> map = new HashMap<>( 16 );
        map.put( "user",user );
        return ApiResult.success(map);
    }

    @ApiOperation( "登录" )
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public ApiResult<Map<String,String>> login(@Valid @RequestBody LoginDTO dto){
        String token = umsUserService.login( dto );
        if(ObjectUtils.isEmpty( token )){
            return ApiResult.failed( "账号密码错误" );
        }
        HashMap<String, String> map = new HashMap<>( 16 );
        map.put( "token",token );
        return ApiResult.success(map,"登录成功");
    }

    @ApiOperation( "获取用户信息" )
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    private ApiResult<UmsUser> getUser(@RequestHeader(value = USER_NAME) String userName) {
        UmsUser user = umsUserService.getUserByUsername( userName );
        return ApiResult.success(user);
    }

    @ApiOperation( "注销登录" )
    @GetMapping("/logout")
    public ApiResult logout() {
        return ApiResult.success(null, "注销成功");
    }

    @ApiOperation( "根据名字获取用户文章" )
    @GetMapping("/{username}")
    public ApiResult<Map<String, Object>> getUserByName(@PathVariable("username") String username,
                                                        @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
                                                        @RequestParam(value = "size", defaultValue = "10") Integer size) {
        Map<String, Object> map = new HashMap<>(16);
        UmsUser user = umsUserService.getUserByUsername(username);
        Assert.notNull(user, "用户不存在");
        Page<BmsPost> page = bmsPostService.page(new Page<>(pageNo, size),
                new LambdaQueryWrapper<BmsPost>().eq( BmsPost::getUserId, user.getId()));
        map.put("user", user);
        map.put("posts", page);
        return ApiResult.success(map);
    }

    @ApiOperation( "更新用户" )
    @PostMapping("/update")
    public ApiResult<UmsUser> updateUser(@RequestBody UmsUser umsUser){
        umsUserService.updateById( umsUser );
        return ApiResult.success(umsUser);
    }
}
